package hadoop.topN.question1;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

//需求一：
//（1）切分，获取字段
//（2）先求每个人的成绩（平均分）， 再求科目平均分

public class StuMapper1 extends Mapper<LongWritable, Text,Text, DoubleWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //（1）切分，获取字段
        String[]line=value.toString().split(",");
        //（2）先求每个人的成绩（平均分）
        double sum=0;
        double count=0;
        for(int i=2;i<line.length;i++){
            sum+=Integer.parseInt(line[i]);
            count++;
        }
        String courseName=line[0];
        double selfAvg=sum/count;

        //写入上下文
        context.write(new Text(courseName),new DoubleWritable(selfAvg));
    }
}
